#!/bin/bash
# macOS 网络配置完整恢复脚本

echo "=========================================="
echo "macOS 网络配置恢复工具"
echo "=========================================="
echo "开始时间: $(date)"

# 检查是否以管理员权限运行
if [ "$EUID" -ne 0 ]; then
    echo "⚠️  此脚本需要管理员权限"
    echo "请使用: sudo ./restore.sh"
    exit 1
fi

echo "开始恢复网络配置..."

# 1. 恢复hosts文件
echo "1. 恢复hosts文件..."
if [ -f "hosts.txt" ]; then
    cp hosts.txt /etc/hosts
    echo "   ✅ hosts文件已恢复"
else
    echo "   ❌ 找不到hosts.txt文件"
fi

# 2. 恢复网络偏好设置
echo "2. 恢复网络偏好设置..."
if [ -d "SystemConfiguration" ]; then
    # 备份当前配置
    cp -r /Library/Preferences/SystemConfiguration "/Library/Preferences/SystemConfiguration.backup.$(date +%y%m%d_%H%M%S)"
    echo "   ✅ 当前配置已备份"
    
    # 恢复配置
    cp -r SystemConfiguration/* /Library/Preferences/SystemConfiguration/
    echo "   ✅ 网络偏好设置已恢复"
else
    echo "   ❌ 找不到SystemConfiguration目录"
fi

# 3. 恢复代理设置
echo "3. 恢复代理设置..."
restored_proxies=0

# 恢复HTTP代理
for proxy_file in proxy_web_*.txt; do
    if [ -f "$proxy_file" ]; then
        service_name=$(echo "$proxy_file" | sed 's/proxy_web_//' | sed 's/.txt$//' | sed 's/_/ /g')
        echo "   恢复 $service_name 的HTTP代理..."
        
        if grep -q "Enabled: Yes" "$proxy_file"; then
            server=$(grep "Server:" "$proxy_file" | cut -d' ' -f2)
            port=$(grep "Port:" "$proxy_file" | cut -d' ' -f2)
            if [ ! -z "$server" ] && [ ! -z "$port" ]; then
                networksetup -setwebproxy "$service_name" "$server" "$port"
                echo "     设置HTTP代理: $server:$port"
                ((restored_proxies++))
            fi
        else
            networksetup -setwebproxystate "$service_name" off
            echo "     禁用HTTP代理"
        fi
    fi
done

# 恢复HTTPS代理
for proxy_file in proxy_secure_*.txt; do
    if [ -f "$proxy_file" ]; then
        service_name=$(echo "$proxy_file" | sed 's/proxy_secure_//' | sed 's/.txt$//' | sed 's/_/ /g')
        echo "   恢复 $service_name 的HTTPS代理..."
        
        if grep -q "Enabled: Yes" "$proxy_file"; then
            server=$(grep "Server:" "$proxy_file" | cut -d' ' -f2)
            port=$(grep "Port:" "$proxy_file" | cut -d' ' -f2)
            if [ ! -z "$server" ] && [ ! -z "$port" ]; then
                networksetup -setsecurewebproxy "$service_name" "$server" "$port"
                echo "     设置HTTPS代理: $server:$port"
                ((restored_proxies++))
            fi
        else
            networksetup -setsecurewebproxystate "$service_name" off
            echo "     禁用HTTPS代理"
        fi
    fi
done

# 恢复SOCKS代理
for proxy_file in proxy_socks_*.txt; do
    if [ -f "$proxy_file" ]; then
        service_name=$(echo "$proxy_file" | sed 's/proxy_socks_//' | sed 's/.txt$//' | sed 's/_/ /g')
        echo "   恢复 $service_name 的SOCKS代理..."
        
        if grep -q "Enabled: Yes" "$proxy_file"; then
            server=$(grep "Server:" "$proxy_file" | cut -d' ' -f2)
            port=$(grep "Port:" "$proxy_file" | cut -d' ' -f2)
            if [ ! -z "$server" ] && [ ! -z "$port" ]; then
                networksetup -setsocksfirewallproxy "$service_name" "$server" "$port"
                echo "     设置SOCKS代理: $server:$port"
                ((restored_proxies++))
            fi
        else
            networksetup -setsocksfirewallproxystate "$service_name" off
            echo "     禁用SOCKS代理"
        fi
    fi
done

echo "   ✅ 已恢复 $restored_proxies 个代理配置"

# 4. 恢复其他系统配置
echo "4. 恢复其他系统配置..."
[ -f "ppp_options.txt" ] && cp ppp_options.txt /etc/ppp/options 2>/dev/null && echo "   ✅ PPP配置已恢复"
[ -f "krb5_conf.txt" ] && cp krb5_conf.txt /etc/krb5.conf 2>/dev/null && echo "   ✅ Kerberos配置已恢复"
[ -f "ssh_config.txt" ] && cp ssh_config.txt /etc/ssh/ssh_config 2>/dev/null && echo "   ✅ SSH配置已恢复"

# 5. 重启网络服务
echo "5. 重启网络服务..."
dscacheutil -flushcache
killall -HUP mDNSResponder
echo "   ✅ DNS缓存已刷新"

# 6. 重启网络接口
echo "6. 重启网络接口..."
networksetup -setairportpower en0 off 2>/dev/null
sleep 2
networksetup -setairportpower en0 on 2>/dev/null
echo "   ✅ Wi-Fi接口已重启"

echo "=========================================="
echo "✅ 网络配置恢复完成！"
echo "完成时间: $(date)"
echo "=========================================="
echo ""
echo "建议操作:"
echo "1. 重启系统以确保所有设置完全生效"
echo "2. 重启后测试网络连接"
echo "3. 验证代理设置是否正确"
echo "4. 测试Redis服务器连接（需要先配置公钥）"
echo ""
